












from django.db.models import Q
from django.urls import reverse_lazy
from django.utils import timezone

from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render

# Create your views here.
from django.views.generic import CreateView, UpdateView, ListView, DetailView

from appinput.forms import AppForm
from appinput.models import App
from public.user_group import is_admin_group


class AppInputCreateView(CreateView):
    template_name = 'appinput/create_appinput.html'
    model = App
    form_class = AppForm

    def get(self, request, *args, **kwargs):
        '''
        用来进行权限控制
        :param request:
        :param args:
        :param kwargs:
        :return:
        '''
        if is_admin_group(self.request.user):
            return super().get(request, *args, **kwargs)
        else:
            result = '亲，没有权限，只有管理人员才可以进入'
            return HttpResponse(result)


    def form_invalid(self, form):
        '''
        当用户通过页面提交的表单数据不合法，将会执行此方法
        :param form:
        :return:
        '''
        print(form)
        return self.render_to_response({'form': form})

    def form_valid(self, form):
        '''
        当用户通过页面提交的表单数据合法，将会执行此方法
        :param form:
        :return:
        '''
        App.objects.create(
            name=form.cleaned_data['name'],
            description=form.cleaned_data['description'],
            jenkins_job=form.cleaned_data['jenkins_job'],
            git_url= form.cleaned_data['git_url'],
            build_cmd = form.cleaned_data['build_cmd'],
            package_name= form.cleaned_data['package_name'],
            zip_package_name=form.cleaned_data['zip_package_name'],
            is_restart_stauts=form.cleaned_data['is_restart_stauts'],
            script_url=form.cleaned_data['script_url'],
            manage_user=form.cleaned_data['manage_user'],
        )
        return HttpResponseRedirect(reversed('appinput:list'))

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['now'] = timezone.now()
        context['current_page'] = 'appinput-create'
        context['current_page_name'] = '新增App应用',
        return context

class AppInputUpdateView(UpdateView):
    template_name = 'appinput/edit_appinput.html'
    model = App
    form_class = AppForm

    def get(self, request, *args, **kwargs):
        app_id = request.path.split('/')[-2]
        app_item = App.objects.get(id=app_id)
        if is_admin_group(self.request.user):
            return super().get(request, *args, **kwargs)
        else:
            result = '亲，没有权限，只有管理人员才可以进入'
            return HttpResponse(result)

    def post(self, request, *args, **kwargs):
        app_id = request.path.split('/')[-2]
        app_item = App.objects.get(id=app_id)
        if is_admin_group(self.request.user):
            return super().get(request, *args, **kwargs)
        else:
            result = '亲，没有权限，只有管理人员才可以进入'
            return HttpResponse(result)


    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['current_page'] = 'appinput-edit'
        context['current_page_name'] = '编辑App应用',
        # 什么用法 pk_url_kwarg URLconf 中，包含主键的关键字名称
        context['app_id'] = self.kwargs.get(self.pk_url_kwarg)
        return context

    def get_success_url(self):
        # 这是干嘛的
        return reverse_lazy('appinput:list')

class AppInputListView(ListView):
    template_name = "appinput/list_appinput.html"
    paginate_by = 10

    def get_queryset(self):
        # search_pk 前端定义
        if self.request.GET.get('search_pk'):
            search_pk = self.request.GET.get('search_pk')
            return App.objects.filter(
                Q(name__icontains=search_pk) |
                Q(package_name__icontains=search_pk)
            )
        return App.objects.all()

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['now'] = timezone.now()
        context['is_admin_group'] = is_admin_group(self.request.user)
        context['current_page'] = 'appinput-list'
        context['current_page_name'] = 'App应用列表'
        query_string = self.request.META.get('QUERY_STRING')
        if 'page' in query_string:
            query_list = query_string.split('&')
            query_list = [elem for elem in query_list if not elem.startswith('page')]
            query_string = '?' + '&'.join(query_list) + '&'
        elif query_string is not None:
            query_string = '?' + query_string + '&'
        context['current_url'] = query_string
        return context

class AppInputDetailView(DetailView):
    template_name = 'appinput/detail_appinput.html'
    model = App

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['current_page_name'] = 'App 应用详情'
        context['now'] = timezone.now()
        return context


